<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-user' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="userId" label="ID" align="center" width="40"></el-table-column>
      <el-table-column prop="username" label="账号" align="center" width="120"></el-table-column>
      <el-table-column prop="password" label="密码" align="center" width="100"></el-table-column>
      <el-table-column prop="name" label="昵称" align="center" width="110"></el-table-column>
      <el-table-column prop="gender" label="性别" header-align="center" width="180"></el-table-column>
      <el-table-column prop="phone" label="手机号" header-align="center"></el-table-column>
      <el-table-column prop="email" label="邮箱" header-align="center"></el-table-column>
      <el-table-column prop="intro" label="简介" header-align="center"></el-table-column>
      <el-table-column prop="money" label="余额" header-align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    changeEnable(user) {
      let enableText = ['禁用', '启用'];
      let url = 'http://localhost:8091/user/' + user.id;
      if (user.enable == 1) {
        url += '/enable';
      } else {
        url += '/disable';
      }
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state == 20000) {
          this.$message({
            message: '将【' + user.username + '】的启用状态设置为【' + enableText[user.enable] + '】成功',
            type: 'success'
          });
        } else {
          // this.$message.error(responseBody.message);
          let title = '操作失败';
          this.$alert(responseBody.message, title, {
            confirmButtonText: '确定',
            callback: action => {}
          });
          this.loadUserList();
        }
      });
    },
    handleEdit(user) {

    },
    openDeleteConfirm(user) {
      let title = '提示';
      let message = '此操作将永久删除【' + user.username + '】用户，是否继续？';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(user);
      }).catch(() => {
      });
    },
    handleDelete(user) {
      console.log('handleDelete ... id=' + user.userId);
      let url = 'http://localhost:8091/user/' + user.userId + '/delete';
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        this.loadUserList();
      });
    },
    loadUserList() {
      console.log('loaduserList ...');
      let url = 'http://localhost:8091/user/';
      console.log('url = ' + url);
      this.axios.create({'headers':{'Authorization':localStorage.getItem('jwt')}}).get(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        this.tableData = responseBody.data;
      });
    }
  },
  mounted() {
    this.loadUserList();
  }
}
</script>

<style scoped>

</style>